package com.jml.gmall.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MethodConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.jml.gmall.service.UserService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyDubboConfig {

	//dubbo:application标签  dubbo.application.name=user-service-provider
	public ApplicationConfig  applicationConfig(){
		ApplicationConfig applicationConfig = new ApplicationConfig();
		applicationConfig.setName("user-service-provider");
		return applicationConfig;
	}

	//dubbo:registry    dubbo.registry.address=127.0.0.1:2181
	//dubbo.registry.protocol=zookeeper
	public RegistryConfig registryConfig(){
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setAddress("127.0.0.1:2181");
		registryConfig.setProtocol("zookeeper");
		return registryConfig;
	}

	//dubbo:protocol  dubbo.protocol.name=dubbo
	//dubbo.protocol.port=20881
	public ProtocolConfig protocolConfig(){
		ProtocolConfig protocolConfig = new ProtocolConfig();
		protocolConfig.setName("dubbo");
		protocolConfig.setPort(20881);
		return protocolConfig;
	}

	/*
	*
	* <dubbo:service interface="com.jml.gmall.service.UserService"  ref="userServiceImpl02" timeout="1000" version="2.0.0">
		<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
	</dubbo:service>
	<bean id="userServiceImpl02" class="com.jml.gamll.service.impl.UserServiceImpl2"></bean>
	* */
	@Bean
	public ServiceConfig<UserService> userServiceServiceConfig(UserService userService){
		ServiceConfig<UserService> userServiceServiceConfig = new ServiceConfig<UserService>();
		userServiceServiceConfig.setInterface(UserService.class);
		userServiceServiceConfig.setRef(userService);
		userServiceServiceConfig.setVersion("2.0.0");
		//配置每一个method信息
		List<MethodConfig> methodConfigList=new ArrayList<MethodConfig>();
		MethodConfig methodConfig=new MethodConfig();
		methodConfig.setName("getUserAddressList");
		methodConfig.setTimeout(1000);
		methodConfigList.add(methodConfig);
		//将method的设置保存到service中
		userServiceServiceConfig.setMethods(methodConfigList);
		return userServiceServiceConfig;
	}

	//dubbo.monitor.protocol=registry
	MonitorConfig monitorConfig(){
		MonitorConfig monitorConfig = new MonitorConfig();

		return monitorConfig;
	}

}
